package com.tark.service.qrcode.util.model.utils;


import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.tark.service.qrcode.util.constant.ResultStatusEnum;
import com.tark.service.qrcode.util.model.ApiResult;






/**
 * api 结果构建工具
 * @author moyong
 *
 */
public class ApiResultUtils {
	
	public static ApiResult buildSuccessResult(String url) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		
		return buildApiResult(ResultStatusEnum.OK,"调用成功", null,url,map);
	}

	public static ApiResult buildSuccessApiResult(String message, String url, String bodyKey, Object bodyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);

		if(StringUtils.isNotBlank(bodyKey)) {
			map.put(bodyKey, bodyValue);
		}
		
		return buildApiResult(ResultStatusEnum.OK,message, null,url, map);
	}
	
	public static ApiResult buildSuccessApiResult(String message, String url, Map<String, Object> body) {
		if(body != null) {
			body.put("success", true);
		}

		return buildApiResult(ResultStatusEnum.OK,message, null,url, body);
	}
	
	
	public static ApiResult buildFailedApiResult(String url) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		
		return buildApiResult(ResultStatusEnum.ERROR,"调用失败", null,url,map);
	}
	
	public static ApiResult buildFailedApiResult(String message, String exception, String url, String bodyKey, Object bodyValue) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);

		if(StringUtils.isNotBlank(bodyKey)) {
			map.put(bodyKey, bodyValue);
		}
		
		return buildApiResult(ResultStatusEnum.ERROR, message, exception,url, map);
	}	

	public static ApiResult buildFailedApiResult(String message, String exception, String url, Map<String, Object> body) {
		if(body != null) {
			body.put("success", false);
		}
		
		return buildApiResult(ResultStatusEnum.ERROR, message, exception,url, body);
	}

	public static ApiResult buildApiResult(ResultStatusEnum statusEnum, String message, String exception, String url, Map<String, Object> body) {
		ApiResult apiResult = new ApiResult();
		
		apiResult.setStatusCode(statusEnum.getCode());
		apiResult.setStatusName(statusEnum.getName());
		apiResult.setMessage(message);
		apiResult.setException(exception);
		apiResult.setUrl(url);
		
		if(body != null) {
			apiResult.getBody().putAll(body);
		}

		return apiResult;
	}
	
}
